package vh.android.multiconnector.quiz;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import vh.android.multiconnector.library.model.Channel;
import vh.android.multiconnector.quiz.ui.adapters.ChannelAdapter;
import vh.android.multiconnector.quiz.ui.dialogs.CreateGameDialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {

	private static final String LOG_TAG = MainActivity.class.getName();
	private ChannelAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startService(new Intent(this, MulticastService.class));
		
		ListView lvChannels = (ListView)findViewById(R.id.lvChannelList);
		adapter = new ChannelAdapter(getBaseContext());
		lvChannels.setAdapter(adapter);
		
		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				Map<String, Channel> channels = MulticastService.getInstance().getChannels();
				adapter.addAll(channels.values());
			}
		}, 15000L, 15000L);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.action.menu_create_game:
			DialogFragment f = new CreateGameDialogFragment();
			f.show(getSupportFragmentManager(), "");
			return true;
		}
		return false;
	}
}
